package com.atguigu.eduservice.controller;


import com.atguigu.annotation.WebLog;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.CourseInfoForm;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 陈烨庆
 * @since 2022-01-24
 */
@RestController
@RequestMapping("/eduservice/edu-course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @PostMapping("addCourseInfo")
    @WebLog("添加课程信息")
    public R addCourseInfo(@RequestBody CourseInfoForm courseInfo) {
        String cid = eduCourseService.saveCourseInfo(courseInfo);
        return R.ok().data("courseId", cid);
    }

    @GetMapping("getCourseById/{courseId}")
    @WebLog("查询课程信息")
    public R getCourseById(@PathVariable String courseId) {
        CourseInfoForm courseInfoForm = eduCourseService.getCourse(courseId);
        return R.ok().data("course", courseInfoForm);
    }

    @PostMapping("updateCourseInfo")
    @WebLog("更新课程信息")
    public R updateCourseInfo(@RequestBody CourseInfoForm courseInfo) {
        String cid = eduCourseService.updateCourseInfo(courseInfo);
        return R.ok().data("courseId", cid);
    }

    @GetMapping("getPublishCourseInfo/{courseId}")
    @WebLog("根据课程id查询发布信息")
    public R getPublishCourseInfo(@PathVariable String courseId) {
        CoursePublishVo courseInfo = eduCourseService.getPublishCourseInfo(courseId);
        return R.ok().data("course", courseInfo);
    }

    @PostMapping("publishCourse/{courseId}")
    @WebLog("课程最终发布")
    public R publishCourse(@PathVariable String courseId) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        if (eduCourseService.updateById(eduCourse)) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 查询课程表所有数据
     * rest 风格
     */
    @WebLog("课程列表")
    @ApiOperation("课程列表")
    @GetMapping("findAll")
    public R findAllCourse() {
        // 调用 Service 方法查询所有教师
        List<EduCourse> courseList = eduCourseService.list(null);
        return R.ok().data("items", courseList);
    }

    /**
     * 分页查询课程列表
     */
    @WebLog("分页多条件查询课程列表")
    @ApiOperation("分页多条件查询课程列表")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable long current,
                                      @PathVariable long limit,
                                      @RequestBody(required = false) CourseQuery courseQuery) {
        // page对象
        Page<EduCourse> eduTeacherPage = new Page<>(current, limit);

        // wrapper对象
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        // 多条件组合查询 动态sql
        wrapper.like(!ObjectUtils.isEmpty(courseQuery.getTitle()), "title", courseQuery.getTitle());
        wrapper.eq(!ObjectUtils.isEmpty(courseQuery.getStatus()), "status", courseQuery.getStatus());
        wrapper.orderByDesc("gmt_create");

        // 调用方法分页
        IPage<EduCourse> page = eduCourseService.page(eduTeacherPage, wrapper);
        long total = page.getTotal();
        List<EduCourse> records = page.getRecords();

        return R.ok().data("total", total).data("records", records);
    }

    @DeleteMapping("removeCourseByCourseId/{courseId}")
    @WebLog("删除课程ID")
    public R removeCourseByCourseId(@PathVariable String courseId) {
        if (eduCourseService.removeCourseByCourseId(courseId)) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

